package lzm.aspect;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/*
 * 创建切面类 并且注入spring容器
 */
@Aspect
@Component
public class LogAop {
    //抽取公共的切入点表达式
    //1、本类引用
    //2、其他的切面引用

    // 要拦截的方法：返回值类型 (*代表所有类型)  +包名+方法名(*代表模糊匹配)+参数列表(..代表所有)
    @Pointcut("execution(* lzm.dao.*.*(..))")
    public void pointCut(){};

    @Before("pointCut()")
    public void before(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("切入点"+name+"参数:" +Arrays.asList(args));
    }
    @After("pointCut()")
    public void after(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println(name+"方法执行之后@after");
    }

    @AfterReturning(value="pointCut()",returning="result")
    public void afterReturing(JoinPoint joinPoint , Object result){
        String name = joinPoint.getSignature().getName();
        System.out.println("切入点"+name+"返回值" +result);
    }

    @AfterThrowing(value="pointCut()",throwing="ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
        String name = joinPoint.getSignature().getName();
        System.out.println("切入点"+name+"异常：" +ex);
    }
}